package model;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.statistics.HistogramDataset;

public class BinomialModel {

	private double binomialProbability = 0;
	private double copy;
	private Map<Integer, Double> map = new HashMap<Integer, Double>();
	private DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	
	public void computeBinomialProbability(int x, int n, double P) {
		binomialProbability = combination(n, x) * Math.pow(P, x) * Math.pow(1 - P, n - x);
	}

	public void computeBinomialProbability(int x1, int x2, int n, double P) {
		binomialProbability = 0;
		for(int j = x1; j <= x2; j++) {
			binomialProbability += combination(n, j) * Math.pow(P, j) * Math.pow(1 - P, n - j);
			copy = combination(n, j) * Math.pow(P, j) * Math.pow(1 - P, n - j);
			map.put(j, copy);

			dataset.setValue(copy, "P(X)", (Comparable)j);

		}
	}
	
	public int combination(int total, int choose){
	    if(total < choose)
	        return 0;
	    if(choose == 0 || choose == total)
	        return 1;
	    return combination(total-1,choose-1)+combination(total-1,choose);
	}
	
	public double getBinomialProbability() {
		DecimalFormat df4 = new DecimalFormat("#.####");
		return Double.valueOf(df4.format(binomialProbability));
	}
	
	public CategoryDataset getDataSet() {
		return dataset;
	}
	
	public void clearDataSet() {
		dataset.clear();
	}
	
	public Map getMap() {
		return map;
	}
	
	public void clearMap() {
		map.clear();
	}
	

}

